<template>
  <el-dialog :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" :visible.sync="visible">
    <el-form class="verify-form" :model="dataForm" :rules="dataRule" ref="dataForm" label-width="80px">
      <el-form-item label="赠送商品名称" prop="goodsName">
        <el-input v-model="dataForm.goodsName" placeholder="赠送商品名称" readonly></el-input>
        <el-button type="primary" @click="addGoodsBySingle">单品添加商品</el-button>
      </el-form-item>
      <el-form-item label="商品代码" prop="goodsCode">
        <el-input v-model="dataForm.goodsCode" placeholder="商品代码" readonly></el-input>
      </el-form-item>
      <el-row>
        <el-col :span="12">
          <el-form-item label="赠品简称" prop="giftAbbreviation">
            <el-input v-model="dataForm.giftAbbreviation" placeholder="赠品简称"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label=" " prop="isShow">
            <el-checkbox v-model="dataForm.isShow" :true-label="1" :false-label="0">赠品简称显示在销售页面</el-checkbox>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="21">
          <el-form-item label="查询部门">
            <el-input type="textarea" v-model="dataForm.deptNames" disabled></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="3">
          <el-form-item label-width="0">
            <el-button size="mini" @click="openDeptDialog">选择</el-button>
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item label="赠品类型" prop="giftType">
        <el-select v-model="dataForm.giftType" style="width:180px;">
          <el-option label="通用赠品类(存放于库房,或加工中心的赠品,根据出仓配置消减库存)" :value="1"></el-option>
          <el-option label="门店赠品类(存放与门店赠品,结算时消减库存)" :value="2"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="备注">
        <el-input v-model="dataForm.memo" placeholder="备注"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit" :loading="ischeck" v-prevent-re-click>确定</el-button>
    </span>
    <!-- 弹窗, 商品查询 -->
    <goods-list v-if="goodsListVisible" ref="sinleGoodsList" @updateForm="updateFormData"></goods-list>
    <!-- 弹窗, 活动门店 -->
    <search-dept v-if="deptVisible" ref="deptDialog" @refreshData="getDeptData"></search-dept>
  </el-dialog>
</template>

<script>
  import { getObj, addObj, putObj } from '@/api/eims/giftsmanage'
  import SearchDept from '../setmeal/SearchDept'
  import GoodsList from './goods-search-single'
  export default {
    data() {
      return {
        ischeck: false,
        visible: false,
        goodsListVisible: false,
        deptVisible: false,
        dataForm: {
          id: 0,
          goodsId: '',
          goodsName: '',
          goodsCode: '',
          giftAbbreviation: '',
          giftType: 1,
          deptNames: [],
          deptIds: [],
          isShow: '',
          memo: '',
          auditStatus: '',
        },
        dataRule: {
          goodsName: [{
            required: true,
            message: '赠送商品名称不能为空',
            trigger: 'blur'
          }],
          goodsCode: [{
            required: true,
            message: '赠送商品代码不能为空',
            trigger: 'blur'
          }],
          giftAbbreviation: [{
            required: true,
            message: '赠品简称不能为空',
            trigger: 'blur'
          }],
          giftType: [{
            required: true,
            message: '赠品类型不能为空',
            trigger: 'blur'
          }],
          memo: [{
            required: true,
            message: '备注不能为空',
            trigger: 'blur'
          }]
        }
      }
    },
    components: {
      GoodsList,
      SearchDept
    },
    methods: {
      init(id) {
        this.dataForm.id = id || 0
        this.visible = true
        if (this.dataForm.id == 0) {
          for (var key in this.dataForm) {
            if (key != "giftType") {
              this.dataForm[key] = ''
            }
          }
        }
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            getObj(this.dataForm.id).then(response => {
              if(response.data.code == '0'){
                this.dataForm = response.data.data
              }else{
                this.$message.error(response.data.msg)
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit() {
        if (this.dataForm.deptIds.length <= 0) {
          this.$message.warning("请选择参与活动门店")
          return
        }
        this.ischeck = true
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            if (this.dataForm.id) {
              putObj(this.dataForm).then(response => {
                if(response.data.code == '0'){
                  this.$message.success('修改成功')
                  this.visible = false
                  this.$emit('refreshDataList')
                }else{
                  this.$message.error(response.data.msg)
                }
                this.ischeck = false
              }).catch(() => {
                this.ischeck = false
              })
            } else {
              addObj(this.dataForm).then(response => {
                if(response.data.code == '0'){
                  this.$message.success('添加成功')
                  this.visible = false
                  this.$emit('refreshDataList')
                }else{
                  this.$message.error(response.data.msg)
                }
                this.ischeck = false
              }).catch(() => {
                this.ischeck = false
              })
            }
          }else{
            this.ischeck = false
          }
        })
      },
      //  打开查询部门弹窗
      openDeptDialog() {
        this.deptVisible = true
        this.$nextTick(() => {
          this.$refs.deptDialog.init()
        })
      },
      //  获取选择部门数据
      getDeptData(data) {
        let ids = []
        let names = []
        if (data.length) {
          data.forEach(item => {
            ids.push(item.id)
            names.push(item.name)
          })
        }
        this.dataForm.deptNames = names
        this.dataForm.deptIds = ids
      },
      // 打开商品列表弹窗
      addGoodsBySingle() {
        this.goodsListVisible = true;
        this.$nextTick(() => {
          this.$refs.sinleGoodsList.init({});
        });
      },
      //更新表单数据
      updateFormData(data) {
        this.dataForm.goodsId = data.data.id
        this.dataForm.goodsName = data.data.viewGoodsName
        this.dataForm.goodsCode = data.data.code
      },
    }
  }
</script>
